package com.bgds.sys.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bgds.sys.api.bean.mars.dto.*;
import com.bgds.sys.api.bean.mars.vo.WaveDataVO;
import com.bgds.sys.api.service.DeTongService;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.utils.DateUtils;
import com.bgds.sys.common.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.msgpack.core.MessagePack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 振动控制层
 *
 * @Author: warm
 * @Description:
 * @Date: 2020/7/28 15:52
 * @Version: 1.0
 */
@Slf4j
@RestController
@Api(tags = "德通数据接口")
@RequestMapping("/mars")
public class MarsController {
    @Autowired
    private DeTongService deTongService;

    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ApiOperation(value = "德通设备树", notes = "德通设备树", httpMethod = "GET")
    public Result<List<TreeDto>> tree() {
        List<TreeDto> treeDtos = deTongService.tree();
        return new ResultUtil<List<TreeDto>>().setData(treeDtos);
    }

    @RequestMapping(value = "/nodes/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据 id 获取节点详情", notes = "根据 id 获取节点详情", httpMethod = "GET")
    public Result<NodeDto> nodes(@PathVariable("id") String id) {
        NodeDto nodeDtos = deTongService.listNodeById(id);
        return new ResultUtil<NodeDto>().setData(nodeDtos);
    }



    @RequestMapping(value = "/node/history_data", method = RequestMethod.GET)
    @ApiOperation(value = "节点历史数据", notes = "节点历史数据", httpMethod = "GET")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "nodeId", value = "设备relationId", required = true),
            @ApiImplicitParam(name = "code", required = true, value = "100:表示温度数据，1：高加长波形，11：高加波形，12：低加波形，15：速度波形，101：低频总值，112：高频总值；107：速度总值"),
            @ApiImplicitParam(name = "startTime", value = "startTime", required = true),
            @ApiImplicitParam(name = "endTime", value = "endTime", required = true),
            @ApiImplicitParam(name = "pageIndex", value = "pageIndex"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize")}
    )
    public Result<HistoryDataVo> getNodeHistoryData(@RequestParam(value = "nodeId", required = true) String nodeId,
                                                    @RequestParam(value = "code", required = true) Integer code,
                                                    @RequestParam(value = "startTime", required = true) Long startTime,
                                                    @RequestParam(value = "endTime", required = true) Long endTime,
                                                    @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                                    @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        String end = DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss", new Date(endTime));
        String start = DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss", new Date(startTime));
        HistoryDataVo hisData = deTongService.getHisData(nodeId, code, start, end, pageIndex, pageSize);
        return new ResultUtil<HistoryDataVo>().setData(hisData);
    }

    @RequestMapping(value = "/node/historydata/count", method = RequestMethod.GET)
    @ApiOperation(value = "节点历史数据条数", notes = "节点历史数据条数", httpMethod = "GET")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "nodeId", value = "设备relationId", required = true),
            @ApiImplicitParam(name = "code", required = true, value = "100:表示温度数据，1：高加长波形，11：高加波形，12：低加波形，15：速度波形，101：低频总值，112：高频总值；107：速度总值"),
            @ApiImplicitParam(name = "startTime", value = "startTime", required = true),
            @ApiImplicitParam(name = "endTime", value = "endTime", required = true)
    }
    )
    public Result<Integer> nodeHistoryDataCount(@RequestParam(value = "nodeId", required = true) String nodeId,
                                                @RequestParam(value = "code", required = true) Integer code,
                                                @RequestParam(value = "startTime") Long startTime,
                                                @RequestParam(value = "endTime") Long endTime) {
        String end = DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss", new Date(endTime));
        String start = DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss", new Date(startTime));
        Integer count = deTongService.getHisCount(nodeId, code, start, end);
        return new ResultUtil<Integer>().setData(count);
    }

    @RequestMapping(value = "/node/wave_data", method = RequestMethod.GET)
    @ApiOperation(value = "波形数据", notes = "波形数据", httpMethod = "GET")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "nodeId", value = "设备relationId", required = true),
            @ApiImplicitParam(name = "code", value = "100:表示温度数据，1：高加长波形，11：高加波形，12：低加波形，15：速度波形，101：低频总值，112：高频总值；107：速度总值", required = true),
            @ApiImplicitParam(name = "time", value = "采样时间:历史数据time值 如格式：2021/04/06 09:26:56", required = true),
            @ApiImplicitParam(name = "type", value = "1:时域波形 2:倒谱图", required = true)}
    )
    public void nodeWaveData(@RequestParam(value = "nodeId", required = true) String nodeId,
                             @RequestParam(value = "code", required = true) Integer code,
                             @RequestParam(value = "time", required = true) String time,
                             @RequestParam(value = "type", required = true) Integer type,
                             HttpServletResponse response) {
        Date timeDate = DateUtils.stringConvertDate("yyyy/MM/dd HH:mm:ss", time);
        deTongService.getWaveData(nodeId, code, DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss", timeDate), type, response);
    }

    @RequestMapping(value = "/node/spectrum_data", method = RequestMethod.GET)
    @ApiOperation(value = "趋势波形频谱", notes = "趋势波形频谱", httpMethod = "GET")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "nodeId", value = "设备relationId", required = true),
            @ApiImplicitParam(name = "code", value = "100:表示温度数据，1：高加长波形，11：高加波形，12：低加波形，15：速度波形，101：低频总值，112：高频总值；107：速度总值", required = true),
            @ApiImplicitParam(name = "time", value = "采样时间:历史数据time值 如格式：2021/04/06 09:26:56", required = true)}
    )
    public void getSpectrumData(@RequestParam(value = "nodeId", required = true) String nodeId,
                                @RequestParam(value = "code", required = true) Integer code,
                                @RequestParam(value = "time", required = true) String time, HttpServletResponse response) {
        Date timeDate = DateUtils.stringConvertDate("yyyy/MM/dd HH:mm:ss", time);
        deTongService.getSpectrumData(nodeId, code, DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss", timeDate), response);
    }

    @RequestMapping(value = "/node/envelope_data", method = RequestMethod.GET)
    @ApiOperation(value = "包络数据", notes = "包络数据", httpMethod = "GET")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "nodeId", value = "设备relationId", required = true),
            @ApiImplicitParam(name = "code", value = "100:表示温度数据，1：高加长波形，11：高加波形，12：低加波形，15：速度波形，101：低频总值，112：高频总值；107：速度总值", required = true),
            @ApiImplicitParam(name = "time", value = "采样时间:历史数据time值 如格式：2021/04/06 09:26:56", required = true),
            @ApiImplicitParam(name = "centerFreq", value = "中心频率", required = true),
            @ApiImplicitParam(name = "filterWidth", value = "带宽", required = true)}
    )
    public void getEnvelopeData(@RequestParam(value = "nodeId", required = true) String nodeId,
                                @RequestParam(value = "code", required = true) Integer code,
                                @RequestParam(value = "time", required = true) String time,
                                @RequestParam(value = "centerFreq", required = true) Double centerFreq,
                                @RequestParam(value = "filterWidth", required = true) Double filterWidth,
                                HttpServletResponse response) {
        Date timeDate = DateUtils.stringConvertDate("yyyy/MM/dd HH:mm:ss", time);
        deTongService.getEnvelopeData(nodeId, code, DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss", timeDate), centerFreq, filterWidth, response);
    }

    @RequestMapping(value = "/alarm/page", method = RequestMethod.GET)
    @ApiOperation(value = "获取分页报警", notes = "获取分页报警", httpMethod = "GET")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "detailCount", value = "详情数量（不填则默认为3）", required = false),
            @ApiImplicitParam(name = "pageIndex", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true)}
    )
    public Result<IPage<AlarmEventDto>> page(
            @RequestParam(value = "detailCount", required = false, defaultValue = "3") Integer detailCount,
            @RequestParam(value = "pageIndex", required = true) Integer pageIndex,
            @RequestParam(value = "pageSize", required = true) Integer pageSize
    ) {
        return new ResultUtil<IPage<AlarmEventDto>>().setData(deTongService.pageAlarmEventDto(detailCount, pageIndex, pageSize));
    }




    @RequestMapping(value = "/alarm/{relationId}", method = RequestMethod.GET)
    @ApiOperation(value = "获取单个设备报警", notes = "获取单个设备报警", httpMethod = "GET")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "relationId",paramType = "path",value = "第三方设备关联Id", required = true),
            @ApiImplicitParam(name = "detailCount", value = "详情数量（不填则默认为3）", required = false),
            @ApiImplicitParam(name = "startTime", value = "开始时间(默认1999/01/01 00:00:00)", required = false),
            @ApiImplicitParam(name = "endTime", value = "结束时间(默认2222/01/01 00:00:00)", required = false)}
    )
    public Result<List<AlarmDeviceDto>> queryAlarmByDeviceId(
            @PathVariable(value = "relationId") String relationId,
            @RequestParam(value = "detailCount", required = false, defaultValue = "3") Integer detailCount,
            @RequestParam(value = "startTime",required = false,defaultValue = "1999/01/01 00:00:00") String startTime,
            @RequestParam(value = "endTime", required = false,defaultValue = "2222/01/01 00:00:00") String endTime
    ) {
        List<AlarmDeviceDto> alarmEventDtos=  deTongService.pageAlarmByDeviceId(relationId,detailCount,startTime,endTime);
        return new ResultUtil<List<AlarmDeviceDto>>().setData(alarmEventDtos);
    }
}